//
//  ApplePayTool.m
//  Runner
//
//  Created by mini2021 on 2025/6/23.
//

#import "ApplePayTool.h"
#import <BTHelp/BTApplePay.h>

@interface ApplePayTool()<ApplePayDelegate>

@property (nonatomic, strong) NSString * productId;
@property (nonatomic, strong) NSString * orderId;
@property (nonatomic, strong) NSString * uid;
@property (nonatomic, copy) BTCommonStrBlock callback;

@end

@implementation ApplePayTool

+ (nonnull ApplePayTool*)share{
    static ApplePayTool * config = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        config = [[super allocWithZone:NULL] init];
    });
    return config;
}

+ (id)allocWithZone:(struct _NSZone *)zone {
   return [ApplePayTool share] ;
}


- (id)copyWithZone:(NSZone *)zone {
    return [ApplePayTool share];
}

- (id)mutableCopyWithZone:(NSZone *)zone {
    return [ApplePayTool share];
}

- (void)startPayWithDataStr:(nullable NSString*)dataStr callback:(BTCommonStrBlock)callback{
    self.callback = callback;
    if (BTIsEmptyStr(dataStr)) {
        [self failWithInfo:@"flutter bridge data is empty"];
        return;
    }
    BTDictTool * dictTool = [BTDictTool createWithDict:dataStr];
    NSString * pid = [dictTool getStringWithKey:@"productId"];
    NSString * orderId = [dictTool getStringWithKey:@"orderSn"];
    NSString * uid = [dictTool getStringWithKey:@"uid"];
    if (![BTApplePay.share isCanMakePayments]) {
        [self failWithInfo:@"device don't support apple pay"];
        return;
    }
    [self startPayWithProductId:pid uid:uid orderId:orderId];
}

- (void)startPayWithProductId:(NSString*)productId
                          uid:(NSString*)uid
                      orderId:(NSString*)orderId{
    self.orderId = orderId;
    self.productId = productId;
    self.uid = uid;
    BTApplePay.share.delegate = self;
    [BTApplePay.share clearCurrentProductData];
    [BTApplePay.share requestProducts:@[productId]];
}

- (void)successWithReceipt:(NSString*)receipt{
    NSDictionary * dict = @{
        @"isSuc" : @(YES),
        @"errorInfo" : @"",
        @"receipt" : BTSafeStr(receipt),
    };
    NSString * dictStr = [BTUtils convertDictToJsonStr:dict];
    if (self.callback) {
        self.callback(dictStr);
    }
}

- (void)failWithInfo:(NSString*)info{
    NSDictionary * dict = @{
        @"isSuc" : @(NO),
        @"errorInfo" : BTSafeStr(info),
        @"receipt" : @"",
    };
    NSString * dictStr = [BTUtils convertDictToJsonStr:dict];
    if (self.callback) {
        self.callback(dictStr);
    }
}

- (void)restore:(BTCommonStrBlock)callback{
    self.callback = callback;
    BTApplePay.share.delegate = self;
    [BTApplePay.share restore];
}

#pragma mark ApplePayDelegate
//获取商品信息成功回调
- (void)btApplePayRequestSuccess:(NSArray*)dictArray{
    [BTApplePay.share buy:self.productId uid:self.uid orderId:self.orderId];
}

- (void)btApplePayRequestFail:(NSError*)error{
    [self failWithInfo:error.localizedDescription];
}

//请求完成回调，可以不用处理，如果成功会先调用BTApplePayRequestSuccess再调用此方法
- (void)btApplePayRequestFinish{
    
}

//购买失败回调
- (void)btApplePayBuyFail:(NSString*)failInfo{
    [self failWithInfo:failInfo];
}

//购买成功回调
- (void)btApplePayBuySuccess:(NSString*)payment{
    [self successWithReceipt:payment];
}

//获取当前的支付order id，不实现则在票据缓存中为空
- (NSString*)btApplePaySucGetCurrentOrderId{
    return self.orderId;
}

//恢复购买失败
- (void)btApplePayRestoreFail:(NSError*)error{
    [self failWithInfo:error.localizedDescription];
}

//恢复购买成功
- (void)btApplePayRestoreSuc:(NSString*)payment{
    [self successWithReceipt:payment];
}

@end
